package com.meteor.downloader.videodownloader.activity.set

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.meteor.downloader.videodownloader.R
import com.meteor.downloader.videodownloader.activity.MdmBindingActivity
import com.meteor.downloader.videodownloader.adapter.AdapterHelpItem
import com.meteor.downloader.videodownloader.databinding.ActivityMdmHelpYouBinding
import com.meteor.downloader.videodownloader.mdmdata.DataHelpMdm

class MdmHelpYouActivity : MdmBindingActivity<ActivityMdmHelpYouBinding>() {
    private val mHelpDownloadList = mutableListOf<DataHelpMdm>()
    private lateinit var mAdapterHelpItem: AdapterHelpItem
    private var mNextCount = 0
    override fun beginViewAndDoMdm() {
        mStarNativeValue = true
        mNativeBookLayout = mMdmBinding.flMdmNativeView
        mMdmBinding.layoutTopSet.tvTitleAll.text = getString(R.string.mdm_help)
        setMdmRecyclerView()
        mMdmBinding.tvGoHome.setOnClickListener {
            if (mNextCount < 3) {
                mNextCount++
                mMdmBinding.rvHelpContent.scrollToPosition(mNextCount)
                changeHelpPosition(mNextCount)
            } else {
                finish()
            }
        }
    }

    override fun setMdmRecyclerView() {
        val ti1 = String.format(getString(R.string.mdm_type_help), getString(R.string.app_x))
        val ti2 =
            String.format(getString(R.string.mdm_type_help), getString(R.string.app_pinterest))

        val di1 =
            String.format(getString(R.string.mdm_open_type_select_link), getString(R.string.app_x))
        val di2 = String.format(
            getString(R.string.mdm_open_type_select_link),
            getString(R.string.app_pinterest)
        )
        mHelpDownloadList.add(
            DataHelpMdm(
                ti1,
                getString(R.string.mdm_step_01),
                di1,
                R.drawable.mdm_til_help_step_1
            )
        )
        mHelpDownloadList.add(
            DataHelpMdm(
                ti1,
                getString(R.string.mdm_step_02),
                getString(R.string.mdm_open_app_download),
                R.drawable.mdm_til_help_step_2
            )
        )
        mHelpDownloadList.add(
            DataHelpMdm(
                ti2,
                getString(R.string.mdm_step_01),
                di2,
                R.drawable.mdm_til_help_step_3
            )
        )
        mHelpDownloadList.add(
            DataHelpMdm(
                ti2,
                getString(R.string.mdm_step_02),
                getString(R.string.mdm_open_app_download),
                R.drawable.mdm_til_help_step_4
            )
        )
        mAdapterHelpItem = AdapterHelpItem(this, mHelpDownloadList)
        mMdmBinding.rvHelpContent.apply {
            layoutManager = LinearLayoutManager(
                this@MdmHelpYouActivity, LinearLayoutManager.HORIZONTAL, false
            )
            adapter = mAdapterHelpItem
        }
        val onePageHelp = PagerSnapHelper()
        onePageHelp.attachToRecyclerView(mMdmBinding.rvHelpContent)
        mMdmBinding.rvHelpContent.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    val snapView = onePageHelp.findSnapView(recyclerView.layoutManager)
                    snapView?.let {
                        val position = recyclerView.getChildAdapterPosition(it)
                        changeHelpPosition(position)
                    }
                }
            }
        })
    }

    private fun changeHelpPosition(position: Int) {
        mNextCount = position
        mMdmBinding.pointView.changeMdmPosition(mNextCount)
        if (mNextCount == 3) {
            mMdmBinding.tvGoHome.text = getString(R.string.mdm_end)
        } else {
            mMdmBinding.tvGoHome.text = getString(R.string.mdm_next)
        }
    }


    override fun getLayoutValue(): ActivityMdmHelpYouBinding {
        return ActivityMdmHelpYouBinding.inflate(layoutInflater)
    }
}